//
//  PlanTypeView.m
//  WanCaiC8
//
//  Created by tangang on 2018/3/7.
//  Copyright © 2018年 tangang. All rights reserved.
//

#import "PlanTypeView.h"
#import "LabCell.h"
#import "PlanTypeArrowView.h"
#import "HomePageVM.h"

@interface PlanTypeView()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic ,strong) UICollectionView *collectionView;

@end

@implementation PlanTypeView{
    BOOL    isOpen;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)init{
    if (self=[super init]) {
        self.backgroundColor=DEFAULTBGCOLOR;
        [self addSubview:self.collectionView];
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self).with.insets(UIEdgeInsetsMake(0, 0, 5, 0));
        }];
    }
    return self;
}

- (instancetype)initWithId:(NSString *)Id{
    if (self=[self init]) {
        [self getPlayType:Id];
    }
    return self;
}

- (void)getPlayType:(NSString *)Id{
    WS(weakSelf);
    [[HomePageVM manage] getPlayList:Id andSuccess:^(id returnValue) {
        weakSelf.arrData=returnValue;
        if (weakSelf.selectLType) {
            weakSelf.selectLType(weakSelf.arrData.firstObject);
        }
    } andFail:^(NSString *errorMessage, NSError *error) {
        
    }];
}

- (void)setArrData:(NSArray *)arrData{
    _arrData=arrData;
    [self.collectionView reloadData];
}

- (void)setSelectRow:(NSInteger)selectRow{
    _selectRow=selectRow;
    [self.collectionView reloadData];
}

#pragma mark - collectionView代理函数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (isOpen) {
        return self.arrData.count;
    }
    return self.arrData.count<=4?self.arrData.count:4;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LabCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"LabCell" forIndexPath:indexPath];
    if ([self.arrData[indexPath.row] isKindOfClass:[LotteryTypeModel class]]) {
        LotteryTypeModel *model=self.arrData[indexPath.row];
        [cell setBGColorWithTitle:model.Name andCornerRadius:10 andIsSelect:indexPath.row==self.selectRow];
    }else{
        LotteryPlayModel *model=self.arrData[indexPath.row];
        [cell setBGColorWithTitle:model.PlayName andCornerRadius:10 andIsSelect:indexPath.row==self.selectRow];
    }
    
    
    [cell.labTitle setFont:[UIFont systemFontOfSize:12]];
    return cell;
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 10, 10, 10);
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    self.selectRow=indexPath.row;
    if (self.changeValue) {
        self.changeValue(self.selectRow);
    }
    if (self.selectLType) {
        LotteryPlayModel *model=self.arrData[indexPath.row];
        model.index=indexPath.row;
        self.selectLType(model);
    }
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if ([kind isEqualToString:UICollectionElementKindSectionFooter]) {
        PlanTypeArrowView *header = [collectionView dequeueReusableSupplementaryViewOfKind: UICollectionElementKindSectionFooter withReuseIdentifier:@"PlanTypeArrowView" forIndexPath:indexPath];
        [header setOpen:isOpen];
        WS(weakSelf);
        header.openBlock = ^(BOOL open) {
            isOpen=open;
            [weakSelf.collectionView reloadData];
            if (weakSelf.changeOpen) {
                weakSelf.changeOpen(isOpen,weakSelf.arrData);
            }
        };
        return header;
    }
    
    return nil;
}

- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        [layout setItemSize:CGSizeMake((SCREEN_WIDTH-60)/4, 20)];
        [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
        layout.minimumInteritemSpacing = 0.0;
        layout.minimumLineSpacing = 20.0;
        layout.footerReferenceSize=CGSizeMake(SCREEN_WIDTH, 20);
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsHorizontalScrollIndicator=NO;
        [_collectionView registerClass:[LabCell class] forCellWithReuseIdentifier:@"LabCell"];
        [_collectionView registerClass:[PlanTypeArrowView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"PlanTypeArrowView"];
    }
    return _collectionView;
}


@end
